home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / sunau.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  18KB  |  542 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """Stuff to parse Sun and NeXT audio files.
  5.  
  6. An audio file consists of a header followed by the data.  The structure
  7. of the header is as follows.
  8.  
  9.         +---------------+
  10.         | magic word    |
  11.         +---------------+
  12.         | header size   |
  13.         +---------------+
  14.         | data size     |
  15.         +---------------+
  16.         | encoding      |
  17.         +---------------+
  18.         | sample rate   |
  19.         +---------------+
  20.         | # of channels |
  21.         +---------------+
  22.         | info          |
  23.         |               |
  24.         +---------------+
  25.  
  26. The magic word consists of the 4 characters '.snd'.  Apart from the
  27. info field, all header fields are 4 bytes in size.  They are all
  28. 32-bit unsigned integers encoded in big-endian byte order.
  29.  
  30. The header size really gives the start of the data.
  31. The data size is the physical size of the data.  From the other
  32. parameters the number of frames can be calculated.
  33. The encoding gives the way in which audio samples are encoded.
  34. Possible values are listed below.
  35. The info field currently consists of an ASCII string giving a
  36. human-readable description of the audio file.  The info field is
  37. padded with NUL bytes to the header size.
  38.  
  39. Usage.
  40.  
  41. Reading audio files:
  42.         f = sunau.open(file, 'r')
  43. where file is either the name of a file or an open file pointer.
  44. The open file pointer must have methods read(), seek(), and close().
  45. When the setpos() and rewind() methods are not used, the seek()
  46. method is not  necessary.
  47.  
  48. This returns an instance of a class with the following public methods:
  49.         getnchannels()  -- returns number of audio channels (1 for
  50.                            mono, 2 for stereo)
  51.         getsampwidth()  -- returns sample width in bytes
  52.         getframerate()  -- returns sampling frequency
  53.         getnframes()    -- returns number of audio frames
  54.         getcomptype()   -- returns compression type ('NONE' or 'ULAW')
  55.         getcompname()   -- returns human-readable version of
  56.                            compression type ('not compressed' matches 'NONE')
  57.         getparams()     -- returns a tuple consisting of all of the
  58.                            above in the above order
  59.         getmarkers()    -- returns None (for compatibility with the
  60.                            aifc module)
  61.         getmark(id)     -- raises an error since the mark does not
  62.                            exist (for compatibility with the aifc module)
  63.         readframes(n)   -- returns at most n frames of audio
  64.         rewind()        -- rewind to the beginning of the audio stream
  65.         setpos(pos)     -- seek to the specified position
  66.         tell()          -- return the current position
  67.         close()         -- close the instance (make it unusable)
  68. The position returned by tell() and the position given to setpos()
  69. are compatible and have nothing to do with the actual position in the
  70. file.
  71. The close() method is called automatically when the class instance
  72. is destroyed.
  73.  
  74. Writing audio files:
  75.         f = sunau.open(file, 'w')
  76. where file is either the name of a file or an open file pointer.
  77. The open file pointer must have methods write(), tell(), seek(), and
  78. close().
  79.  
  80. This returns an instance of a class with the following public methods:
  81.         setnchannels(n) -- set the number of channels
  82.         setsampwidth(n) -- set the sample width
  83.         setframerate(n) -- set the frame rate
  84.         setnframes(n)   -- set the number of frames
  85.         setcomptype(type, name)
  86.                         -- set the compression type and the
  87.                            human-readable compression type
  88.         setparams(tuple)-- set all parameters at once
  89.         tell()          -- return current position in output file
  90.         writeframesraw(data)
  91.                         -- write audio frames without pathing up the
  92.                            file header
  93.         writeframes(data)
  94.                         -- write audio frames and patch up the file header
  95.         close()         -- patch up the file header and close the
  96.                            output file
  97. You should set the parameters before the first writeframesraw or
  98. writeframes.  The total number of frames does not need to be set,
  99. but when it is set to the correct value, the header does not have to
  100. be patched up.
  101. It is best to first set all parameters, perhaps possibly the
  102. compression type, and then write audio frames using writeframesraw.
  103. When all frames have been written, either call writeframes('') or
  104. close() to patch up the sizes in the header.
  105. The close() method is called automatically when the class instance
  106. is destroyed.
  107. """
  108. AUDIO_FILE_MAGIC = 779316836
  109. AUDIO_FILE_ENCODING_MULAW_8 = 1
  110. AUDIO_FILE_ENCODING_LINEAR_8 = 2
  111. AUDIO_FILE_ENCODING_LINEAR_16 = 3
  112. AUDIO_FILE_ENCODING_LINEAR_24 = 4
  113. AUDIO_FILE_ENCODING_LINEAR_32 = 5
  114. AUDIO_FILE_ENCODING_FLOAT = 6
  115. AUDIO_FILE_ENCODING_DOUBLE = 7
  116. AUDIO_FILE_ENCODING_ADPCM_G721 = 23
  117. AUDIO_FILE_ENCODING_ADPCM_G722 = 24
  118. AUDIO_FILE_ENCODING_ADPCM_G723_3 = 25
  119. AUDIO_FILE_ENCODING_ADPCM_G723_5 = 26
  120. AUDIO_FILE_ENCODING_ALAW_8 = 27
  121. AUDIO_UNKNOWN_SIZE = 0xFFFFFFFFL
  122. _simple_encodings = [
  123.     AUDIO_FILE_ENCODING_MULAW_8,
  124.     AUDIO_FILE_ENCODING_LINEAR_8,
  125.     AUDIO_FILE_ENCODING_LINEAR_16,
  126.     AUDIO_FILE_ENCODING_LINEAR_24,
  127.     AUDIO_FILE_ENCODING_LINEAR_32,
  128.     AUDIO_FILE_ENCODING_ALAW_8]
  129.  
  130. class Error(Exception):
  131.     pass
  132.  
  133.  
  134. def _read_u32(file):
  135.     x = 0x0L
  136.     for i in range(4):
  137.         byte = file.read(1)
  138.         if byte == '':
  139.             raise EOFError
  140.         byte == ''
  141.         x = x * 256 + ord(byte)
  142.     
  143.     return x
  144.  
  145.  
  146. def _write_u32(file, x):
  147.     data = []
  148.     for i in range(4):
  149.         (d, m) = divmod(x, 256)
  150.         data.insert(0, m)
  151.         x = d
  152.     
  153.     for i in range(4):
  154.         file.write(chr(int(data[i])))
  155.     
  156.  
  157.  
  158. class Au_read:
  159.     
  160.     def __init__(self, f):
  161.         if type(f) == type(''):
  162.             import __builtin__ as __builtin__
  163.             f = __builtin__.open(f, 'rb')
  164.         
  165.         self.initfp(f)
  166.  
  167.     
  168.     def __del__(self):
  169.         if self._file:
  170.             self.close()
  171.         
  172.  
  173.     
  174.     def initfp(self, file):
  175.         self._file = file
  176.         self._soundpos = 0
  177.         magic = int(_read_u32(file))
  178.         if magic != AUDIO_FILE_MAGIC:
  179.             raise Error, 'bad magic number'
  180.         magic != AUDIO_FILE_MAGIC
  181.         self._hdr_size = int(_read_u32(file))
  182.         if self._hdr_size < 24:
  183.             raise Error, 'header size too small'
  184.         self._hdr_size < 24
  185.         if self._hdr_size > 100:
  186.             raise Error, 'header size ridiculously large'
  187.         self._hdr_size > 100
  188.         self._data_size = _read_u32(file)
  189.         if self._data_size != AUDIO_UNKNOWN_SIZE:
  190.             self._data_size = int(self._data_size)
  191.         
  192.         self._encoding = int(_read_u32(file))
  193.         if self._encoding not in _simple_encodings:
  194.             raise Error, 'encoding not (yet) supported'
  195.         self._encoding not in _simple_encodings
  196.         if self._encoding in (AUDIO_FILE_ENCODING_MULAW_8, AUDIO_FILE_ENCODING_ALAW_8):
  197.             self._sampwidth = 2
  198.             self._framesize = 1
  199.         elif self._encoding == AUDIO_FILE_ENCODING_LINEAR_8:
  200.             self._framesize = self._sampwidth = 1
  201.         elif self._encoding == AUDIO_FILE_ENCODING_LINEAR_16:
  202.             self._framesize = self._sampwidth = 2
  203.         elif self._encoding == AUDIO_FILE_ENCODING_LINEAR_24:
  204.             self._framesize = self._sampwidth = 3
  205.         elif self._encoding == AUDIO_FILE_ENCODING_LINEAR_32:
  206.             self._framesize = self._sampwidth = 4
  207.         else:
  208.             raise Error, 'unknown encoding'
  209.         self._framerate = self._encoding in (AUDIO_FILE_ENCODING_MULAW_8, AUDIO_FILE_ENCODING_ALAW_8)(_read_u32(file))
  210.         self._nchannels = int(_read_u32(file))
  211.         self._framesize = self._framesize * self._nchannels
  212.         if self._hdr_size > 24:
  213.             self._info = file.read(self._hdr_size - 24)
  214.             for i in range(len(self._info)):
  215.                 if self._info[i] == '\x00':
  216.                     self._info = self._info[:i]
  217.                     break
  218.                     continue
  219.             
  220.         else:
  221.             self._info = ''
  222.  
  223.     
  224.     def getfp(self):
  225.         return self._file
  226.  
  227.     
  228.     def getnchannels(self):
  229.         return self._nchannels
  230.  
  231.     
  232.     def getsampwidth(self):
  233.         return self._sampwidth
  234.  
  235.     
  236.     def getframerate(self):
  237.         return self._framerate
  238.  
  239.     
  240.     def getnframes(self):
  241.         if self._data_size == AUDIO_UNKNOWN_SIZE:
  242.             return AUDIO_UNKNOWN_SIZE
  243.         if self._encoding in _simple_encodings:
  244.             return self._data_size / self._framesize
  245.         return 0
  246.  
  247.     
  248.     def getcomptype(self):
  249.         if self._encoding == AUDIO_FILE_ENCODING_MULAW_8:
  250.             return 'ULAW'
  251.         if self._encoding == AUDIO_FILE_ENCODING_ALAW_8:
  252.             return 'ALAW'
  253.         return 'NONE'
  254.  
  255.     
  256.     def getcompname(self):
  257.         if self._encoding == AUDIO_FILE_ENCODING_MULAW_8:
  258.             return 'CCITT G.711 u-law'
  259.         if self._encoding == AUDIO_FILE_ENCODING_ALAW_8:
  260.             return 'CCITT G.711 A-law'
  261.         return 'not compressed'
  262.  
  263.     
  264.     def getparams(self):
  265.         return (self.getnchannels(), self.getsampwidth(), self.getframerate(), self.getnframes(), self.getcomptype(), self.getcompname())
  266.  
  267.     
  268.     def getmarkers(self):
  269.         pass
  270.  
  271.     
  272.     def getmark(self, id):
  273.         raise Error, 'no marks'
  274.  
  275.     
  276.     def readframes(self, nframes):
  277.         if self._encoding in _simple_encodings:
  278.             if nframes == AUDIO_UNKNOWN_SIZE:
  279.                 data = self._file.read()
  280.             else:
  281.                 data = self._file.read(nframes * self._framesize * self._nchannels)
  282.             if self._encoding == AUDIO_FILE_ENCODING_MULAW_8:
  283.                 import audioop as audioop
  284.                 data = audioop.ulaw2lin(data, self._sampwidth)
  285.             
  286.             return data
  287.  
  288.     
  289.     def rewind(self):
  290.         self._soundpos = 0
  291.         self._file.seek(self._hdr_size)
  292.  
  293.     
  294.     def tell(self):
  295.         return self._soundpos
  296.  
  297.     
  298.     def setpos(self, pos):
  299.         if pos < 0 or pos > self.getnframes():
  300.             raise Error, 'position not in range'
  301.         pos > self.getnframes()
  302.         self._file.seek(pos * self._framesize + self._hdr_size)
  303.         self._soundpos = pos
  304.  
  305.     
  306.     def close(self):
  307.         self._file = None
  308.  
  309.  
  310.  
  311. class Au_write:
  312.     
  313.     def __init__(self, f):
  314.         if type(f) == type(''):
  315.             import __builtin__
  316.             f = __builtin__.open(f, 'wb')
  317.         
  318.         self.initfp(f)
  319.  
  320.     
  321.     def __del__(self):
  322.         if self._file:
  323.             self.close()
  324.         
  325.  
  326.     
  327.     def initfp(self, file):
  328.         self._file = file
  329.         self._framerate = 0
  330.         self._nchannels = 0
  331.         self._sampwidth = 0
  332.         self._framesize = 0
  333.         self._nframes = AUDIO_UNKNOWN_SIZE
  334.         self._nframeswritten = 0
  335.         self._datawritten = 0
  336.         self._datalength = 0
  337.         self._info = ''
  338.         self._comptype = 'ULAW'
  339.  
  340.     
  341.     def setnchannels(self, nchannels):
  342.         if self._nframeswritten:
  343.             raise Error, 'cannot change parameters after starting to write'
  344.         self._nframeswritten
  345.         if nchannels not in (1, 2, 4):
  346.             raise Error, 'only 1, 2, or 4 channels supported'
  347.         nchannels not in (1, 2, 4)
  348.         self._nchannels = nchannels
  349.  
  350.     
  351.     def getnchannels(self):
  352.         if not self._nchannels:
  353.             raise Error, 'number of channels not set'
  354.         self._nchannels
  355.         return self._nchannels
  356.  
  357.     
  358.     def setsampwidth(self, sampwidth):
  359.         if self._nframeswritten:
  360.             raise Error, 'cannot change parameters after starting to write'
  361.         self._nframeswritten
  362.         if sampwidth not in (1, 2, 4):
  363.             raise Error, 'bad sample width'
  364.         sampwidth not in (1, 2, 4)
  365.         self._sampwidth = sampwidth
  366.  
  367.     
  368.     def getsampwidth(self):
  369.         if not self._framerate:
  370.             raise Error, 'sample width not specified'
  371.         self._framerate
  372.         return self._sampwidth
  373.  
  374.     
  375.     def setframerate(self, framerate):
  376.         if self._nframeswritten:
  377.             raise Error, 'cannot change parameters after starting to write'
  378.         self._nframeswritten
  379.         self._framerate = framerate
  380.  
  381.     
  382.     def getframerate(self):
  383.         if not self._framerate:
  384.             raise Error, 'frame rate not set'
  385.         self._framerate
  386.         return self._framerate
  387.  
  388.     
  389.     def setnframes(self, nframes):
  390.         if self._nframeswritten:
  391.             raise Error, 'cannot change parameters after starting to write'
  392.         self._nframeswritten
  393.         if nframes < 0:
  394.             raise Error, '# of frames cannot be negative'
  395.         nframes < 0
  396.         self._nframes = nframes
  397.  
  398.     
  399.     def getnframes(self):
  400.         return self._nframeswritten
  401.  
  402.     
  403.     def setcomptype(self, type, name):
  404.         if type in ('NONE', 'ULAW'):
  405.             self._comptype = type
  406.         else:
  407.             raise Error, 'unknown compression type'
  408.         return type in ('NONE', 'ULAW')
  409.  
  410.     
  411.     def getcomptype(self):
  412.         return self._comptype
  413.  
  414.     
  415.     def getcompname(self):
  416.         if self._comptype == 'ULAW':
  417.             return 'CCITT G.711 u-law'
  418.         if self._comptype == 'ALAW':
  419.             return 'CCITT G.711 A-law'
  420.         return 'not compressed'
  421.  
  422.     
  423.     def setparams(self, .1):
  424.         (nchannels, sampwidth, framerate, nframes, comptype, compname) = .1
  425.         self.setnchannels(nchannels)
  426.         self.setsampwidth(sampwidth)
  427.         self.setframerate(framerate)
  428.         self.setnframes(nframes)
  429.         self.setcomptype(comptype, compname)
  430.  
  431.     
  432.     def getparams(self):
  433.         return (self.getnchannels(), self.getsampwidth(), self.getframerate(), self.getnframes(), self.getcomptype(), self.getcompname())
  434.  
  435.     
  436.     def tell(self):
  437.         return self._nframeswritten
  438.  
  439.     
  440.     def writeframesraw(self, data):
  441.         self._ensure_header_written()
  442.         nframes = len(data) / self._framesize
  443.         if self._comptype == 'ULAW':
  444.             import audioop
  445.             data = audioop.lin2ulaw(data, self._sampwidth)
  446.         
  447.         self._file.write(data)
  448.         self._nframeswritten = self._nframeswritten + nframes
  449.         self._datawritten = self._datawritten + len(data)
  450.  
  451.     
  452.     def writeframes(self, data):
  453.         self.writeframesraw(data)
  454.         if self._nframeswritten != self._nframes or self._datalength != self._datawritten:
  455.             self._patchheader()
  456.         
  457.  
  458.     
  459.     def close(self):
  460.         self._ensure_header_written()
  461.         if self._nframeswritten != self._nframes or self._datalength != self._datawritten:
  462.             self._patchheader()
  463.         
  464.         self._file.flush()
  465.         self._file = None
  466.  
  467.     
  468.     def _ensure_header_written(self):
  469.         if not self._nframeswritten:
  470.             if not self._nchannels:
  471.                 raise Error, '# of channels not specified'
  472.             self._nchannels
  473.             if not self._sampwidth:
  474.                 raise Error, 'sample width not specified'
  475.             self._sampwidth
  476.             if not self._framerate:
  477.                 raise Error, 'frame rate not specified'
  478.             self._framerate
  479.             self._write_header()
  480.         
  481.  
  482.     
  483.     def _write_header(self):
  484.         if self._comptype == 'NONE':
  485.             if self._sampwidth == 1:
  486.                 encoding = AUDIO_FILE_ENCODING_LINEAR_8
  487.                 self._framesize = 1
  488.             elif self._sampwidth == 2:
  489.                 encoding = AUDIO_FILE_ENCODING_LINEAR_16
  490.                 self._framesize = 2
  491.             elif self._sampwidth == 4:
  492.                 encoding = AUDIO_FILE_ENCODING_LINEAR_32
  493.                 self._framesize = 4
  494.             else:
  495.                 raise Error, 'internal error'
  496.         self._sampwidth == 1
  497.         if self._comptype == 'ULAW':
  498.             encoding = AUDIO_FILE_ENCODING_MULAW_8
  499.             self._framesize = 1
  500.         else:
  501.             raise Error, 'internal error'
  502.         self._framesize = (self._comptype == 'ULAW')._framesize * self._nchannels
  503.         _write_u32(self._file, AUDIO_FILE_MAGIC)
  504.         header_size = 25 + len(self._info)
  505.         header_size = header_size + 7 & -8
  506.         _write_u32(self._file, header_size)
  507.         if self._nframes == AUDIO_UNKNOWN_SIZE:
  508.             length = AUDIO_UNKNOWN_SIZE
  509.         else:
  510.             length = self._nframes * self._framesize
  511.         _write_u32(self._file, length)
  512.         self._datalength = length
  513.         _write_u32(self._file, encoding)
  514.         _write_u32(self._file, self._framerate)
  515.         _write_u32(self._file, self._nchannels)
  516.         self._file.write(self._info)
  517.         self._file.write('\x00' * (header_size - len(self._info) - 24))
  518.  
  519.     
  520.     def _patchheader(self):
  521.         self._file.seek(8)
  522.         _write_u32(self._file, self._datawritten)
  523.         self._datalength = self._datawritten
  524.         self._file.seek(0, 2)
  525.  
  526.  
  527.  
  528. def open(f, mode = None):
  529.     if mode is None:
  530.         if hasattr(f, 'mode'):
  531.             mode = f.mode
  532.         else:
  533.             mode = 'rb'
  534.     
  535.     if mode in ('r', 'rb'):
  536.         return Au_read(f)
  537.     if mode in ('w', 'wb'):
  538.         return Au_write(f)
  539.     raise Error, "mode must be 'r', 'rb', 'w', or 'wb'"
  540.  
  541. openfp = open
  542.